import { 
  AmbientLight,
  BoxBufferGeometry,
  Mesh,
  MeshStandardMaterial,
  PerspectiveCamera,
  Scene, 
  TextureLoader, 
  WebGLRenderer
} from "three";
import stats from '../../utils/stats';
// 调用
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import { GUI } from 'dat.gui';

let scene = new Scene();
let renderer = setRenderer();
let camera = setCamera();

const controls = {
  normalScaleX: 1,
  normalScaleY: 1,
  rotationSpeed: 0.01
}

setLight();
init();
addGUI();

const trackballControls = new OrbitControls(camera, renderer.domElement);
// 调整场景位置
trackballControls.target.set(0, 3, 0);

function render() {
  stats.update();
  trackballControls.update();

  requestAnimationFrame(render);
  renderer.render(scene, camera);
}
render();

function setRenderer() {
  const renderer = new WebGLRenderer({ antialias: true });
  renderer.setPixelRatio(window.devicePixelRatio);
  renderer.setSize(window.innerWidth, window.innerHeight);
  // 支持阴影生成 
  renderer.shadowMap.enabled = true;
  document.body.appendChild(renderer.domElement);

  return renderer
}

function setCamera(): PerspectiveCamera {
  let camera = new PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);
  camera.position.set(0, 20, 40);
  scene.add( camera );

  return camera;
}

// 灯光设置
function setLight() {
  scene.add(new AmbientLight(0xeeeeee));
}

/** 功能区 */
function init() {
  let textureLoader = new TextureLoader();
  let material = new MeshStandardMaterial({
    map: textureLoader.load('/textures/uv/ash_uvgrid01.jpg'),
    metalness: 0.02,
    roughness: 0.07,
    color: 0xffffff,
  });

  let cubeGeom = new BoxBufferGeometry(8, 8, 8);
  let cube = new Mesh(cubeGeom, material);
  scene.add(cube);
  
}


function addGUI(): void {
  const gui = new GUI();  
}